package com.lingo.a2b.common.util;

import com.lingo.a2b.common.util.model.NestedFieldPackBo;
import org.springframework.util.StringUtils;

/**
 * @author lingo
 * @date 2020/5/22 15:39
 * @Company: www.xyb2b.com
 */

public class PackUtils {
    private static final String seperator = ",";


    public static String packNestedFieldName(String preFieldName, String nowFieldName) {
        if (StringUtils.isEmpty(preFieldName)){
            return nowFieldName;
        }
        return String.format("%s%s%s", preFieldName, seperator, nowFieldName);
    }

    public static boolean isNestedFieldName(String str) {
        return !StringUtils.isEmpty(str) && str.contains(seperator);
    }

    public static NestedFieldPackBo unpackNestedFieldName(String str) {
        if (isNestedFieldName(str)) {
            int pos = str.lastIndexOf(seperator);
            NestedFieldPackBo bo = new NestedFieldPackBo();
            bo.setNowFieldName(str.substring(pos + 1));
            bo.setPreFieldName(str.substring(0, pos));
            return bo;
        }
        return null;
    }

    public static String[] split(String packStr) {
        if (isNestedFieldName(packStr)) {
            return StringUtils.split(packStr, seperator);
        }
        return new String[]{packStr};
    }
}
